package com.aprivate.kn.ratingsystem.daoutil;

import android.content.Context;
import android.util.Log;

import com.aprivate.kn.ratingsystem.dao.DaoManager;
import com.aprivate.kn.ratingsystem.daoBean.Project;
import com.aprivate.kn.ratingsystem.dao.ProjectDao;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

/**
 * Created by kenan on 2018/11/27.
 */


public class ProjectDaoUtil {
    private DaoManager mManager;
    private String TAG="flag";
    //操作Fruit的构造函数
    public ProjectDaoUtil(Context context) {
        mManager = DaoManager.getInstance();
        mManager.init(context);
    }
    /**
     * 完成project记录的插入，如果表未创建，先创建project表
     *
     * @param project
     * @return  返回long值 -1为失败
     */
    public boolean insertProject(Project project) {
        boolean flag = false;
        flag = mManager.getDaoSession().getProjectDao().insertOrReplace(project) != -1;
        Log.i(TAG, "-------插入一条的结果为"+flag);
        return flag;
    }

    /**
     * 插入多条数据，在子线程操作
     * @return
     */
    public boolean insertListProject(final List<Project> mList) {
        boolean flag = false;
        try {
            mManager.getDaoSession().runInTx(new Runnable() {
                @Override
                public void run() {
                    for (Project fruit : mList) {
                        mManager.getDaoSession().insertOrReplace(fruit);
                    }
                }
            });
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     *   刷新指定数据
     */
    public boolean reFreshProject(Project project){
        boolean flag = false;
        mManager.getDaoSession().refresh(project);
        return flag;
    }
    /**
     * 修改一条数据
     * @return
     */
    public boolean updateProject(Project project){
        boolean flag = false;
        try {
            mManager.getDaoSession().update(project);
            flag = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 删除单条记录
     * @return
     */
    public boolean deleteProject(Project project){
        boolean flag = false;
        try {
            mManager.getDaoSession().delete(project);
            flag = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 删除所有记录
     * @return
     */
    public boolean deleteAll(){
        boolean flag = false;
        try {
            mManager.getDaoSession().deleteAll(Project.class);
            flag = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 查询所有记录
     * @return
     */
    public List<Project> queryAllProject(){
        return mManager.getDaoSession().loadAll(Project.class);
    }

    /**
     * 根据主键id查询记录
     * @param key
     * @return
     */
    public Project queryProjectById(long key){
        return mManager.getDaoSession().load(Project.class, key);
    }

    /**
     * 使用 sql语句进行查询操作
     *  参数一sql语句  参数二查询条件限定
     */
    public List<Project> queryProjectBySql(String sql, String[] conditions){
        return mManager.getDaoSession().queryRaw(Project.class, sql, conditions);
    }

    /**
     * 使用queryBuilder进行查询
     * @return
     */
    public List<Project> queryPrijectByQueryBuilder(long id){
        QueryBuilder<Project> queryBuilder = mManager.getDaoSession().queryBuilder(Project.class);
        return queryBuilder.where(ProjectDao.Properties.Id.eq(id)).list();
    }
}


